<?php
/**
 * Zead
 *
 * LICENSE	
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to eu@marcelomx.com so we can send you a copy immediately.
 *
 * @category   Zead
 * @author     Marcelo Rodrigues Gonzaga <eu@marcelomx.com>
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga
 * @link       http://code.google.com/p/zead 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License
 *
 **/


/** @see Zead_Html_Filter_Abstract **/
require_once 'Zead/Html/Filter/Abstract.php';

/**
 * @category   Zead
 * @package    Zead_Html
 * @subpackage Zead_Html_Filter
 * @copyright  Copyright (c) 2008 Marcelo Rodrigues Gonzaga 
 * @license    http://www.opensource.org/licenses/bsd-license.php     New BSD License  
 */
class Zead_Html_Filter_Audio extends Zead_Html_Filter_Abstract
{	
	/**
	 * Aplica o filtro, eliminando os objetos DOM que são instruções de processamento.
	 * @param array Os tipos de instruções que podem ser mantidas.
	 */
	public function filter($addClassName = '') {
		if ($addClassName == '') return;

		// Selecionando e aplicando a classe CSS de audio aos icones que possuirem mp3
		// na extensao do arquivo
		$this->html->select('a')
			->select(array($this, 'select'))
			->invoke('addClassName', $addClassName);
	}

	/**
	 * Verificando se um elemento possui no link o atributo href e se o mesmo
	 * é um link para um arquivo de áudio.
	 * @param Zead_Html_Element
	 */
	public static function select($element) 
	{
		$regex = "4mp|669|6cm|8cm|8med|8svx|aac|abc|ac3|acd|act|ahx|aif|aifc|";
		$regex .="aiff|ais|akp|al|alaw|all|amr|ams|ams|ape|ase|au|aud|avr|";
		$regex .="band|bap|bdd|box|bwf|c01|cda|cdr|cel|cmf|copy|cpr|csh|cwp|d00|d01|dcf|";
		$regex .="dcm|dct|dewf|df2|dfc|dig|dig|dls|dm|dmf|dsf|dsm|dsp|dss|dtm|dvf|DWD|ear|efa|";
		$regex .="efe|efk|efq|efs|efv|emd|esps|f2r|f32|f3r|f64|far|fff|flac|flp|fsm|fzb|fzf|fzv|";
		$regex .="g721|g723|g726|gig|gp5|gsm|gsm|ics|iff|ins|ins|it|iti|its|k25|k26|kar|kin|kmp|";
		$regex .="koz|koz|krz|ksc|ksf|ktp|l|lso|lvp|m3u|m4a|m4b|m4p|ma1|mdl|med|mid|midi|miniusf|";
		$regex .="mka|mmf|mod|mp2|mp3|mpa|mpc|mpd|mpu|mp_|msv|mt2|mte|mti|mtm|mtp|mts|mus|mws|nap|";
		$regex .="nrt|nsf|nst|ntn|nwc|ogg|okt|omf|pac|pat|pbf|pca|pcm|phy|pls|prg|prg|psm|ptm|pts|";
		$regex .="pvc|qcp|r|ra|ram|raw|rbs|rfl|rmf|rmi|rmx|rng|rol|rsn|rti|rtm|rts|s3i|s3m|saf|";
		$regex .="sam|sb|sbi|sbk|sc2|sd|sd|sd2|sds|sdx|sf|sf2|sfk|sfl|shn|sib|sid|smf|smp|snd|";
		$regex .="snd|snd|sng|sou|sppack|sprg|sseq|ssnd|stm|stx|svx|sw|swa|syh|syw|syx|td0|tfmx|";
		$regex .="thx|toc|tsp|txw|u|ub|ulaw|ult|ulw|uni|usf|usflib|uw|uwf|vag|val|vmd|vmf|vmf|voc|";
		$regex .="voi|vox|vqf|vrf|w01|wav|wave|wax|wfb|wfd|wfp|wma|wow|wpk|wproj|wrk|wus|wut|wv|wvc|";
		$regex .="wwu|xfs|xi|xm|xmf|xmi|xp|xt|zvd";
			
		if ($element->hasAttribute('href') && ($href = $element->getAttribute('href')) != '')
			if (preg_match("/(mp3)$/", $href))
				return true;

		return false;
	}
}

